home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlib25 / crtinit.c < prev    next >
C/C++ Source or Header  |  1992-10-15  |  12KB  |  437 lines

  1. /*
  2.  *
  3.  * Crtinit: C run-time initialization code.
  4.  * Written by Eric R. Smith, and placed in the public domain.
  5.  * Use at your own risk.
  6.  *
  7.  * 01/03/89 ++jrb
  8.  *    The (new) meaning of _stksize: (thanks to allan pratt for the feedback)
  9.  *
  10.  *    _stksize            meaning
  11.  *      -1L        keep all of memory (except MINFREE at top) and do
  12.  *            mallocs from own heap, with heap grown upwards towards
  13.  *            stack, and the stack growing down towards heap,
  14.  *            with a minimum slush between them so that they
  15.  *            dont meet (only checked while malloc'ing). With
  16.  *            this model, further spawning is not possible, but it is
  17.  *            well suited for programs such as gcc-cc1 etc.
  18.  *            Thanks to Piet van Oostrum & Atze Dijkstra for this idea
  19.  *
  20.  *    0L        keep minimum amount of memory. this is also the
  21.  *            case when _stksize is undefined by the user.
  22.  *    1L        keep 1/4 of memory, free 3/4 ( as in Alcyon GEMSTART)
  23.  *    2L        keep 2/4 (1/2), free rest
  24.  *    3L        keep 3/4, free 1/4
  25.  *    other        keep that many bytes
  26.  *    -other        keep |other| bytes, use the heap for mallocs
  27.  *
  28.  * 02/14/90 ++jrb (thanks edgar)
  29.  *    auto acc detect
  30.  *    undump friendly
  31.  *
  32.  * NOTE: dumping applications should use _initial_stack instead: if
  33.  *     !=0, then _stksize is initialized from _initial_stack, and
  34.  *     mallocs are always from internal heap. (TeX works much better now),
  35.  *     thanks edgar!
  36.  *
  37.  * Acc convention:
  38.  *      Preferred: user sets _stksize, then _heapbase is set to Malloc(_stksize)
  39.  *      sp to _heapbase + _stksize and all mallocs happen from heap
  40.  *
  41.  *      Old way:
  42.  *    user sets _heapbase to bottom of stack + heap area
  43.  *         sets _stksize to the size of this area
  44.  *         at startup, sp will be set to top of this area
  45.  *         (_heapbase  + _stksize ) and malloc()'s will happen from heap.
  46.  *        (note malloc() and *not* Malloc())
  47.  *
  48.  * 02/16/90 ++jrb
  49.  *  - bug fix: dont get screwed by desktop launch when fast bit is set
  50.  *             convert env string to format usable
  51.  *        (atari get your act together!!)
  52.  *
  53.  * 09-20-92 hyc
  54.  *    Support base relative addressing, for shared-text execution.
  55.  *    Also merged in some stuff from ++jrb's crt0.c.
  56.  *
  57.  * Turbo / Pure C version 21-6-92 um
  58.  *   the Turbo / Pure linker reserves stack space in the BSS and sets
  59.  *   the symbol _StkSize to its size. If _StkSize is set to 0, we use
  60.  *   _stksize etc. as usual
  61.  *
  62.  */
  63.  
  64. /* define this symbol to get ARGV argument passing that's strictly
  65.  * compatible with the Atari standard. If it's not defined, then
  66.  * the startup code won't validate the ARGV= variable by checking
  67.  * the command byte for 0x127. Note that there are still some
  68.  * applications (gulam is a notable example) that implement only
  69.  * part of the standard and don't set the command byte to 0x127.
  70.  */
  71.  
  72. #if 0
  73. #define STRICTLY_COMPATIBLE_WITH_STANDARD
  74. #endif
  75.  
  76. #include <basepage.h>
  77. #include <osbind.h>
  78. #include "lib.h"
  79.  
  80. #define isspace(c) ((c) == ' '||(c) == '\t')
  81. #define MINFREE    (8L * 1024L)        /* free at least this much mem */
  82.                     /* on top */
  83. #define MINKEEP (8L * 1024L)        /* keep at least this much mem */
  84.  
  85. BASEPAGE *_base;
  86. char **environ;
  87. static long argc;
  88. static char **argv;
  89.  
  90. /* size to be allocated for the stack */
  91. extern long _stksize;        /* picked up from user or from stksiz.c */
  92.  
  93. /* set to heap base addr when _stksize < 0 || _initial_stack || When DA
  94.  * note that we allow the user to provide a _heapbase of their own, since
  95.  * that used to be necessary for acc's; but that is no longer needed
  96.  * (or encouraged) since the acc startup code now Mallocs a heap
  97.  * automatically
  98.  */
  99. extern void *_heapbase;        /* picked up from user or from heapbase.c */
  100.  
  101. /*
  102.  * initial stack is used primarily by dumping application,
  103.  * if it is, malloc is always from heap, and _stksize is init
  104.  * from initial_stack (to preserve the value in the undumped run)
  105.  */
  106. extern long _initial_stack;    /* picked up from user or from inistack.c */
  107.  
  108. #ifdef __TURBOC__
  109. char *_StkLim;    /* for Turbo / Pure C stack checking */
  110. extern _StkSize; /* set by linker */
  111. #endif
  112.  
  113. /* default sizeof stdio buffers */
  114. size_t __DEFAULT_BUFSIZ__;    /* .comm             */
  115.  
  116. /* are we an app? */
  117. short _app;
  118.  
  119. /* are we on a split addr mem ST */
  120. short _split_mem = 0;
  121.  
  122. static long parseargs    __PROTO((BASEPAGE *));
  123. static void setup_handlers    __PROTO((void));
  124. __EXTERN void _main    __PROTO((long, char **, char **));
  125. __EXTERN void _init_signal    __PROTO((void));
  126. __EXTERN void _monstartup __PROTO((void *lowpc, void *highpc));
  127. __EXTERN void __mcleanup __PROTO((void));
  128. __EXTERN void _moncontrol __PROTO((long));
  129. __EXTERN void _setstack __PROTO((char *));
  130.  
  131. /*
  132.  * accessories start here:
  133.  */
  134.  
  135. static char    *acc_argv[] = {"", (char *) 0}; /* no name and no arguments */
  136.  
  137. void _acc_main()
  138. {
  139. #ifdef __TURBOC__
  140.     if (&_StkSize == 0)
  141.     {
  142. #endif
  143.  
  144.     if (_stksize == 0 || _stksize == -1L)
  145.         _stksize = MINKEEP;
  146.  
  147.     if (_stksize < 0)
  148.         _stksize = -_stksize;
  149.     _stksize &= 0xfffffffeL;    /* stack on word boundary */
  150.  
  151. #ifdef __TURBOC__
  152.     }
  153.     else
  154.     {
  155.         /* This compound statement is executed if the Pure linker
  156.         has reserved space for the stack in the BSS */
  157.         _stksize = (long) &_StkSize;
  158.         _heapbase = _base->p_bbase + _base->p_blen - _stksize;
  159.     }
  160.     _StkLim = (char *) _heapbase + 256;     /* for stack checking */
  161. #endif
  162.   
  163.     if (_heapbase == 0) {
  164.         _heapbase = (void *)Malloc(_stksize);
  165.     }
  166.     _setstack((char *) _heapbase + _stksize);
  167.     _app = 0;                /* this is an accessory */
  168.  
  169.     _main(1L, acc_argv, acc_argv);
  170.     /*NOTREACHED*/
  171. }
  172.  
  173. void _crtinit()
  174. {
  175.     register BASEPAGE *bp;
  176.     register long m;
  177.     register long freemem;
  178. #ifdef __GNUC__
  179.     extern void etext();    /* a "function" to fake out pc-rel addressing */
  180. #endif
  181.     _app = 1;    /* its an application */
  182.     if(!__DEFAULT_BUFSIZ__)
  183.         __DEFAULT_BUFSIZ__ = BUFSIZ;
  184.  
  185.     bp = _base;
  186.  
  187.     m = parseargs(bp);    /* m = # bytes used by environment + args */
  188.  
  189. /* make m the total number of bytes required by program sans stack/heap */
  190.     m += (bp->p_tlen + bp->p_dlen + bp->p_blen + sizeof(BASEPAGE));
  191.     m = (m + 3L) & (~3L);
  192.  
  193. #ifdef __TURBOC__
  194.     if (&_StkSize == 0)
  195.     {
  196. #endif
  197.  
  198. /* freemem the amount of free mem accounting for MINFREE at top */
  199.     if((freemem = (long)bp->p_hitpa - (long)bp - MINFREE - m) <= 0L)
  200.         goto notenough;
  201.     
  202.     if(_initial_stack)
  203.     {
  204.         /* the primary use of _initial_stack will be in dumping */
  205.         /* applications where only a heap for malloc makes sense */
  206.         _heapbase = (void *) ((long)bp + m);
  207.         _stksize = _initial_stack;
  208.     } else {
  209.         if (_stksize >= -1L)
  210.         _split_mem = 1; /* malloc from Malloc first, then from own heap */
  211.     }
  212.  
  213.     if (_stksize == -1L) {
  214.         _stksize = freemem;
  215.         _heapbase = (void *) ((long)bp + m);
  216.     } else if (_stksize == 0L) {    /* free all but MINKEEP */
  217.         _stksize = MINKEEP;
  218.     } else if (_stksize == 1L) {     /* keep 1/4, free 3/4 */
  219.         _stksize = freemem >> 2;
  220.     } else if (_stksize ==  2L) {    /* keep 1/2, free 1/2 */
  221.         _stksize = freemem >> 1;
  222.     } else if (_stksize == 3L) {    /* keep 3/4, free 1/4 */
  223.         _stksize = freemem - (freemem >> 2); 
  224.     } else {
  225.         if(_stksize < -1L) { /* keep |_stksize|, use heap for mallocs */
  226.         _stksize = -_stksize;
  227.         _heapbase = (void *)((long)bp + m);
  228.         }
  229.     }
  230.     
  231. /* make m the total number of bytes including stack */
  232.     _stksize = _stksize & (~3L);
  233.     m += _stksize;
  234.  
  235. /* make sure there's enough room for the stack */
  236.     if (((long)bp + m) > ((long)bp->p_hitpa - MINFREE))
  237.         goto notenough;
  238.  
  239. /* set up the new stack to bp + m  */
  240.  
  241. #ifdef __TURBOC__
  242.     {
  243.         char *tmp;
  244.         tmp = (char *) bp + m;
  245.         _setstack(tmp);
  246.         _StkLim = tmp - _stksize + 256; /* for stack checking */
  247.     }
  248. #else
  249.     _setstack((char *)bp + m);
  250. #endif
  251.  
  252. #ifdef __TURBOC__
  253.     } /* if (&_StkSize == 0) */
  254.     else
  255.     {
  256.         /* This compound statement is executed if the Pure linker
  257.         has reserved space for the stack in the BSS */
  258.         _stksize = (long) &_StkSize;
  259.         _stksize = _stksize & (~3L);
  260.         
  261.         {
  262.             char *tmp;
  263.             tmp = (char *) bp->p_bbase + bp->p_blen;
  264.             _setstack(tmp);
  265.             _StkLim = tmp - _stksize + 256; /* for stack checking */
  266.             _heapbase = NULL; /* no mallocs from heap */
  267.         }
  268.     }
  269. #endif /* __TURBOC__ */
  270.  
  271. /* shrink the TPA */
  272.     (void)Mshrink(bp, m);
  273.  
  274. /* establish handlers,  call the main routine */
  275.     setup_handlers();
  276.  
  277. /* start profiling, if we were linked with gcrt0.o */
  278. #ifdef __GNUC__
  279.     _monstartup((void *)bp->p_tbase, (void *)((long)etext - 1));
  280. #else
  281.     _monstartup((void *)(bp->p_tbase), 
  282.            (void *)((long)bp->p_tbase +  bp->p_tlen));
  283. #endif
  284.  
  285.     _main(argc, argv, environ);
  286.     /* not reached normally */
  287.  
  288. notenough:
  289.     Cconws("Fatal error: insufficient memory\r\n");
  290.         Pterm(-1);
  291. }
  292.  
  293.  
  294. /*
  295.  * parseargs(bp): parse the environment and arguments pointed to by the
  296.  * basepage. Return the number of bytes of environment and arguments
  297.  * that have been appended to the bss area (the environ and argv arrays
  298.  * are put here, as is a temporary buffer for the command line, if
  299.  * necessary).
  300.  *
  301.  * The MWC extended argument passing scheme is assumed.
  302.  *
  303.  */
  304.  
  305. static long parseargs(bp)
  306.     BASEPAGE *bp;
  307. {
  308.     long count = 4;        /* compensate for aligning */
  309.     long  i;
  310.     char *from, *cmdln, *to;
  311.     char **envp, **arg;
  312. /* flag to indicate desktop-style arg. passing */
  313.     long desktoparg = 0;
  314.  
  315. /* handle the environment first */
  316.  
  317.     environ = envp = (char **)(( (long)bp->p_bbase + bp->p_blen + 4) & (~3));
  318.     from = bp->p_env;
  319.     while (*from) {
  320.  
  321. /* if we find MWC arguments, tie off environment here */
  322.         if (*from == 'A' && *(from+1) == 'R' && *(from+2) == 'G' &&
  323.             *(from+3) == 'V' && *(from+4) == '=') {
  324.             *envp++ = (char *) 0; count += 4;
  325.             *from++ = 0;
  326. #ifdef STRICTLY_COMPATIBLE_WITH_STANDARD
  327.             if (bp->p_cmdlin[0] != 127)
  328.                 goto old_cmdlin;
  329. #endif
  330.             while (*from++) ; /* skip ARGV= string */
  331.             argv = arg = envp++;
  332.             *arg++ = from; count+= 4;
  333.             while (*from++) ; /* skip argv[0] */
  334.             goto do_argc;
  335.         }
  336.         *envp++ = from;
  337.         count += 4;
  338.         desktoparg = 1;
  339.         while (*from) {
  340.             if (*from == '=') {
  341.                 desktoparg = 0;
  342.             }
  343.             from++;
  344.         }
  345.         from++;        /* skip 0 */
  346.  
  347. /* the desktop (and some shells) use the environment in the wrong
  348.    way, putting in "PATH=\0C:\0" instead of "PATH=C:". so if we
  349.    find an "environment variable" without an '=' in it, we
  350.    see if the last environment variable ended with '=\0', and
  351.    if so we append this one to the last one
  352.  */
  353.         if(desktoparg && envp > &environ[1]) 
  354.         {
  355.         /* launched from desktop -- fix up env */
  356.             char *p, *q;
  357.  
  358.             q = envp[-2];    /* current one is envp[-1] */
  359.             while (*q) q++;
  360.             if (q[-1] == '=') {
  361.             p = *--envp;
  362.             while(*p)
  363.                *q++ = *p++;
  364.                 *q = '\0';
  365.            }
  366.         }
  367.     }
  368.     *envp++ = (char *)0;
  369.     count += 4;
  370.  
  371. #ifdef STRICTLY_COMPATIBLE_WITH_STANDARD
  372. old_cmdlin:
  373. #endif
  374. /* Allocate some room for the command line to be parsed */
  375.     cmdln = bp->p_cmdlin;
  376.     i = *cmdln++;
  377.     from = to = (char *) envp;
  378.     if (i > 0) {
  379.         count += (i&(~3));
  380.         envp = (char **) ( ((long) envp)  + (i&(~3)) );
  381.     }
  382.     envp += 2; count += 8;
  383.  
  384. /* Now parse the command line and put argv after the environment */
  385.  
  386.     argv = arg = envp;
  387.     *arg++ = "";        /* argv[0] not available */
  388.     count += 4;
  389.     while(i > 0 && isspace(*cmdln) )
  390.         cmdln++,--i;
  391.  
  392.     while (i > 0) {
  393.         if (isspace(*cmdln)) {
  394.             --i; cmdln++;
  395.             while (i > 0 && isspace(*cmdln))
  396.                 --i,cmdln++;
  397.             *to++ = 0;
  398.         }
  399.         else {
  400.             if ((*to++ = *cmdln++) == 0) break;
  401.             --i;
  402.         }
  403.     }
  404.     *to++ = '\0';
  405.     *to = '\0'; /* bug fix example:cmdln == '\3' 'a' ' ' 'b' '\0' */
  406.     /* the loop below expects \0\0 at end to terminate! */
  407.     /* the byte @ cmdln[i+2] != 0 when fast bit is set */
  408. do_argc:
  409.     argc = 1;        /* at this point argv[0] is done */
  410.     while (*from) {
  411.         *arg++ = from;
  412.         argc++;
  413.         count += 4;
  414.         while(*from++) ;
  415.     }
  416.     *arg++ = (char *) 0;
  417.     return count+4;
  418. }
  419.  
  420. static void setup_handlers()
  421. {
  422.     _init_signal();
  423. }
  424.  
  425. /*
  426.  * _exit: does the actual exiting. Note that _moncontrol and __mcleanup are
  427.  * dummies in crt0.s, but do something in gcrt0.s
  428.  */
  429.  
  430. void _exit(status)
  431.     int status;
  432. {
  433.     _moncontrol(0L);
  434.     __mcleanup();
  435.     Pterm(status);
  436. }
  437.